package jmine.tec.web.wicket.component.command.button;

import java.util.List;

import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;

/**
 * @author Rafael Volpato
 * @date Aug 2, 2011
 */
public class ButtonCommandsPanel extends Panel {

    private final List<ButtonCommand> commands;

    /**
     * Construtor
     * 
     * @param id id do painel
     * @param commands lista de comandos que deve ser exibidos no painel
     */
    public ButtonCommandsPanel(String id, List<ButtonCommand> commands) {
        super(id);
        this.commands = commands;
        ListView<ButtonCommand> lv = new ListView<ButtonCommand>("elements", this.commands) {
            @Override
            protected void populateItem(ListItem<ButtonCommand> item) {
                final ButtonCommand command = item.getModelObject();
                Button link = command.createButtonComponent("cell");
                item.add(link);
            }
        };
        this.add(lv);
    }

    /**
     * Returns the index of the command that is an instance of the given class
     * 
     * @param commandClass commandClass
     * @return index
     */
    public int getCommandIndex(Class<? extends ButtonCommand> commandClass) {
        for (int i = 0; i < this.commands.size(); i++) {
            if (commandClass.isInstance(this.commands.get(i))) {
                return i;
            }
        }
        return -1;
    }
}
